const app = getApp();

Page({

    data: {
        userinfo: {},
        session_key: ''
    },
    onLoad: function () {
        var self = this;
        if (!app.globalData.session_key) {
            wx.login({
                success: res => {
                    // 发送 res.code 到后台换取 openId, sessionKey, unionId
                    wx.request({
                        url: app.globalData.ApiUrl + '/index.php?act=connect&op=wechat_jscode',
                        data: {
                            js_code: res.code
                        },
                        method: 'GET',
                        dataType: 'json',
                        success: (res) => {
                            self.setData({
                                session_key: res.data.datas.session_key
                            })
                        },
                    });
                }
            });
        } else {
            self.setData({
                session_key: app.globalData.session_key
            })
        }
        wx.getSetting({
            success: res => {
                if (res.authSetting['scope.userInfo']) {
                    // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                    wx.getUserInfo({
                        success: res => {
                            // 可以将 res 发送给后台解码出 unionId
                            wx.request({
                                url: app.globalData.ApiUrl + '/index.php?act=connect&op=wechat_get_unionid',
                                method: 'POST',
                                data: {
                                    encryptedData: res.encryptedData,
                                    iv: res.iv,
                                    session_key: self.data.session_key,
                                },
                                header: {
                                    'content-type': 'application/x-www-form-urlencoded'
                                },
                                success: res => {
                                    if (res.data.code == 200) {
                                        self.setData({
                                            userinfo: res.data.datas
                                        })
                                    } else {
                                        wx.showToast({
                                            title: res.data.datas.error,
                                            icon: 'none'
                                        })
                                    }

                                }
                            })
                        }
                    })
                }
            }
        })
    },
    bdyLogin: function () {
        wx.navigateTo({
            url: '../bdylogin/bdylogin'
        });
    },
    wechatLogin: function () {
        var self = this;
        //调用获取unionid接口
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=connect&op=wechat_login',
            data: {
                unionid: self.data.userinfo.unionId,
                avatarUrl: self.data.userinfo.avatarUrl,
                nickName: self.data.userinfo.nickName
            },
            method: 'GET',
            success: (res) => {
                if (res.data.code == 200) {
                    if (res.data.datas.key == 0) {
                        wx.navigateTo({
                            url: '../member_register/member_register?nickName=' + self.data.userinfo.nickName + '&avatarUrl=' + self.data.userinfo.avatarUrl + "&unionid=" + self.data.userinfo.unionId
                        })
                    } else if (res.data.datas.key != 0) {
                        wx.setStorage({
                            key: 'key',
                            data: res.data.datas.key
                        })
                        wx.setStorage({
                            key: 'username',
                            data: res.data.datas.username
                        })
                        wx.setStorage({
                            key: 'member_mobile',
                            data: res.data.datas.member_mobile
                        })
                        self.getCartNum(res.data.datas.key);
                        setTimeout(function () {
                            wx.navigateBack({
                                delta: 1
                            })
                        }, 200)
                        wx.showLoading({
                            title: '登录中',
                        })
                    }
                } else {
                    wx.showToast({
                        title: res.data.datas.error,
                        icon: 'none'
                    })
                }
            }
        });
    },
    //获取购物车商品数
    getCartNum: function (key) {
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=member_cart&op=cart_goods_count',
            data: {
                key: key
            },
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            dataType: 'json',
            success: (res) => {
                if (res.data.code == 200) {
                    var result = res.data.datas;
                    if (result.cart_count > 0) {
                        wx.setTabBarBadge({
                            index: 3,
                            text: String(result.cart_count)
                        })
                    } else {
                        wx.removeTabBarBadge({
                            index: 3,
                        })
                    }

                    wx.setStorage({
                        key: 'cart_count',
                        data: result.cart_count
                    })
                }
            }
        });
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        wx.hideLoading()
    }
})